package com.turman.glsample.trangle_fan

import android.opengl.GLES20
import android.opengl.GLSurfaceView
import com.turman.glsample.MyApplication
import com.turman.glsample.R
import com.turman.glsample.base.loadShader
import com.turman.glsample.base.readStringFromRaw
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class TrangleFanRenderer : GLSurfaceView.Renderer {
    companion object {
        private const val POSITION_COMPONENT_COUNT = 2
        private const val COLOR_COMPONENT_COUNT = 3
        private const val BYTES_PER_FLOAT = 4
    }

    private val tableVerticesWithTriangles: FloatArray = floatArrayOf(
        // 属性的顺序: X, Y, R, G, B,A
        // 三角形扇形
        0f, 0f, 1f, 1f, 1f,
        -0.5f, -0.5f, 0.7f, 0.7f, 0.7f,
        0.5f, -0.5f, 0.7f, 0.7f, 0.7f,
        0.5f, 0.5f, 0.7f, 0.7f, 0.7f,
        -0.5f, 0.5f, 0.7f, 0.7f, 0.7f,
        -0.5f, -0.5f, 0.7f, 0.7f, 0.7f,
        // 中线
        -0.5f, 0f, 1f, 0f, 0f,
        0.5f, 0f, 1f, 0f, 0f,
        // 两个木槌
        0f, -0.25f, 0f, 0f, 1f,
        0f, 0.25f, 1f, 0f, 0f
    )

    //创建一个native内存用于存储顶点数据
    private val vertexData: FloatBuffer = ByteBuffer
        .allocateDirect(tableVerticesWithTriangles.size * BYTES_PER_FLOAT)
        .order(ByteOrder.nativeOrder())
        .asFloatBuffer()
        .put(tableVerticesWithTriangles)

    private var programId: Int = -1

    override fun onSurfaceCreated(p0: GL10?, p1: EGLConfig?) {
        GLES20.glClearColor(0F, 0F, 0F, 1F) //清理颜色 红
//        GLES20.glEnable(GLES20.GL_CULL_FACE)
//        GLES20.glEnable(GLES20.GL_DEPTH_TEST)

        //读取shader代码
        val vertexShaderCode =
            MyApplication.application?.readStringFromRaw(R.raw.trangle_fan_vertex_shader)
        val fragmentShaderCode =
            MyApplication.application?.readStringFromRaw(R.raw.trangle_fan_fragment_shader)

        //创建程序
        if (GLES20.glIsProgram(programId)) {
            GLES20.glDeleteProgram(programId)
            programId = -1
        }
        programId = GLES20.glCreateProgram()

        //给程序设置shader
        GLES20.glAttachShader(programId, loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode!!))
        GLES20.glAttachShader(
            programId,
            loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode!!)
        )
        GLES20.glBindAttribLocation(programId, 0, "a_Position")
        GLES20.glBindAttribLocation(programId, 0, "a_Color")
        //链接程序
        GLES20.glLinkProgram(programId)
        //验证程序
        GLES20.glValidateProgram(programId)

    }

    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
        GLES20.glViewport(0, 0, width, height)
    }

    override fun onDrawFrame(gl: GL10?) {
//        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT or GLES20.GL_DEPTH_BUFFER_BIT)

        GLES20.glUseProgram(programId)
        val aPositionLocation = GLES20.glGetAttribLocation(programId, "a_Position")
        GLES20.glEnableVertexAttribArray(aPositionLocation) //需要启用下属性
        vertexData.position(0) //指定读取起始位置
        GLES20.glVertexAttribPointer(
            aPositionLocation, //关联的shader中的属性
            POSITION_COMPONENT_COUNT,
            GLES20.GL_FLOAT,
            false,
            (POSITION_COMPONENT_COUNT + COLOR_COMPONENT_COUNT) * BYTES_PER_FLOAT, //这里指定的是间隔多长距离读取
            vertexData
        )
        val aColorLocation = GLES20.glGetAttribLocation(programId, "a_Color")
        GLES20.glEnableVertexAttribArray(aColorLocation)
        vertexData.position(POSITION_COMPONENT_COUNT)
        GLES20.glVertexAttribPointer(
            aColorLocation,
            COLOR_COMPONENT_COUNT,
            GLES20.GL_FLOAT,
            false,
            (POSITION_COMPONENT_COUNT + COLOR_COMPONENT_COUNT) * BYTES_PER_FLOAT,
            vertexData
        )

        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 6)
        GLES20.glDrawArrays(GLES20.GL_LINES, 6, 2)
        GLES20.glDrawArrays(GLES20.GL_POINTS, 8, 1)
        GLES20.glDrawArrays(GLES20.GL_POINTS, 9, 1)

        GLES20.glDisableVertexAttribArray(aPositionLocation)
        GLES20.glDisableVertexAttribArray(aColorLocation)
    }
}